package com.binguo.thirdPay.ZhenYuanPay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.binguo.thirdPay.MD5Util;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author yuritian
 * @Title: ZhenYuanPay
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-08-1409:26
 */
@Service
public class ZhenYuanPay {

    private final String URL = "http://221.12.45.206:8080/easytong_app/";

    private final String APPID = "C9E1AF96109E41D08AF7D3DB8C4AD909";

    private final String APPSECRET = "791D4457F73FABE92F6F75C6DF6E0DF0";

    private final String TOKEN = "313664613037616462366217EBE77F42D9BC278C4FCC7804862D74E9CE27E62354F101091C97504C375DEA6BD4EFCE";

    public static void main(String[] args) {
        ZhenYuanPay zhenYuanPay = new ZhenYuanPay();
        zhenYuanPay.getaccount("e1e0cf7c000000000000","");
    }

    public JSONObject parseqrcode(String qrCode,String accessToken){
        SortedMap<String, Object> param = new TreeMap<>();
        param.put("qrCode",qrCode);
        JSONObject httpResult = httpZhenYuan(param,"infoqueryservice/parseqrcode",TOKEN);
        String code = httpResult.getString("code");
        if (code.equals("0")) {
            JSONObject data = httpResult.getJSONObject("data");
            return data;
        }else {
            return null;
        }
    }

    public void qrcodepay(String qrCode,String accNum,String accessToken){
        SortedMap<String, Object> param = new TreeMap<>();
        param.put("qrCode",qrCode);
        param.put("accNum",accNum);
        JSONObject result = httpZhenYuan(param,"payservice/qrcodepay",TOKEN);
    }

    public JSONObject getaccount(String uniqueId,String accessToken){
        SortedMap<String, Object> param = new TreeMap<>();
        param.put("queryType","4");
        param.put("uniqueId",uniqueId);
        JSONObject httpResult = httpZhenYuan(param,"infoqueryservice/getaccount",TOKEN);
        String code = httpResult.getString("code");
        if (code.equals("0")) {
            JSONObject data = httpResult.getJSONObject("data");
            return data;
        }else {
            return null;
        }
    }

    public JSONObject pay(String accNum,String amount,String serialNum,String accessToken){
        SortedMap<String, Object> param = new TreeMap<>();
        param.put("accNum",accNum);
        param.put("amount",amount);
        param.put("serialNum",serialNum);
        JSONObject result = httpZhenYuan(param,"payservice/pay",TOKEN);
        String code = result.getString("code");
        if (code.equals("0")) {
            JSONObject data = result.getJSONObject("data");
            return data;
        }else {
            return null;
        }
    }



    private JSONObject httpZhenYuan(SortedMap<String, Object> param, String method,String accessToken){
        String sign = "";
        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap map = new LinkedMultiValueMap();
        for (Map.Entry<String, Object> entry : param.entrySet()) {
            sign += entry.getKey() + "=" + entry.getValue() + "&";
            map.add(entry.getKey(),entry.getValue());
        }

        sign += "key="+APPID;
        System.out.println(sign);
        map.add("sign", MD5Util.MD5Upper32(sign));
        System.out.println(JSON.toJSONString(map));
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity requestBody = new HttpEntity(map, headers);
        System.out.println(URL+"api/"+method+"?access_token="+accessToken);
        ResponseEntity<String> resp = restTemplate.postForEntity(URL+"api/"+method+"?access_token="+accessToken, requestBody, String.class);
        String result = resp.getBody();
        JSONObject resultJson = JSONObject.parseObject(result);
        System.out.println(result);
        return resultJson;
    }
}
